/*************************************************
 * HISTORY:                                      *
 *	06/22/2020 pyx : Created.                    *
 *	06/01/2022 pyx : Edited.                     *
=================================================*/
#include "DATransformerUtil.h"

// IMPORTANT! ONLY FOR LINUX!

std::vector<std::string> listJsonFileName(const char *dirname)
{
    assert(dirname[0] == '/'); // FIXME

    std::vector<std::string> res;

    DIR *dp;
    struct dirent *dirp;
    struct stat st;

    /* open dirent directory */
    if ((dp = opendir(dirname)) == NULL)
    {
        perror("opendir");
        return res;
    }

    /**
     * read all files in this dir
     **/
    while ((dirp = readdir(dp)) != NULL)
    {
        char fullname[255];
        memset(fullname, 0, sizeof(fullname));

        /* ignore hidden files */
        if (dirp->d_name[0] == '.')
            continue;

        // printf("%s\n",dirp->d_name);

        strncpy(fullname, dirname, sizeof(fullname));
        strncat(fullname, "/", sizeof(fullname));
        strncat(fullname, dirp->d_name, sizeof(fullname));
        /* get dirent status */
        bool broken = false;
        if (stat(fullname, &st) == -1)
        {
            broken = true;
            // THIS FILE IS BROKEN ,<IGNORE IT>
            perror("stat");
            printf("ERROR: INVALID STATUS OF FILE:%s\n", fullname);

            // exit(-1);
        }

        printf("%s\n", fullname);

        std::string filepath = fullname;

        std::string::size_type pos = filepath.rfind(".");
        if (pos != std::string::npos)
        {
            std::string suffix = filepath.substr(pos + 1);
            if (suffix == "DAjson")
            {
                if (broken)
                {
                    printf("ERROR: DAJSON FILE BROKEN!name: %s, please rerun DA tool!\n", fullname);
                    exit(-1);
                }
                // filepath = "\'" + filepath;
                // filepath += "\'";

                res.push_back(filepath);
                std::cout << filepath << std::endl;
            }
        }

        /* if dirent is a directory, call itself */
        if (S_ISDIR(st.st_mode))
        {
            std::vector<std::string> child = listJsonFileName(fullname);

            if (!child.empty())
                res.insert(res.end(), child.begin(), child.end());
        }
    }

    closedir(dp);
    return res;
}

void split(const std::string &s, std::vector<std::string> &tokens, char delim)
{
    tokens.clear();
    auto string_find_first_not = [s, delim](size_t pos = 0) -> size_t
    {
        for (size_t i = pos; i < s.size(); i++)
        {
            if (s[i] != delim)
                return i;
        }
        return std::string::npos;
    };
    size_t lastPos = string_find_first_not(0);
    size_t pos = s.find(delim, lastPos);
    while (lastPos != std::string::npos)
    {
        tokens.emplace_back(s.substr(lastPos, pos - lastPos));
        lastPos = string_find_first_not(pos);
        pos = s.find(delim, lastPos);
    }
}

std::string getCWD()
{

    char path[PATH_SIZE];
    if (!getcwd(path, PATH_SIZE))
    {
        std::cout << "Get path fail!"
                  << "\n";
        exit(1);
    }
    std::string directory = path;

    return directory;
}

void trim(std::string &s)
{
    if (s.empty())
    {
        return;
    }
    s.erase(0, s.find_first_not_of(" "));
    s.erase(s.find_last_not_of(" ") + 1);
}